package com.tz.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tz.entity.Cart;
import com.tz.entity.Category;
import com.tz.entity.Commodity;
import com.tz.entity.Product;
import com.tz.service.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Namespace("/product1")
public class ProductAction extends ActionSupport {
    private List<Category> categoryList;
    private List<Product> products;
    private long category_id;
    private double totalTax;
    private double totalDiscount;
    private long product_id;
    private String type;
    @Autowired
    private ProductService productService;


    //ProductBysService1 productBysService= MyAcUtil.getAc().getBean(ProductBysService1.class);
    //ProductBysService1 categoryService= MyAcUtil.getAc().getBean(ProductBysService1.class);
    public ProductAction() {
       // Map<String, Object> application = ActionContext.getContext().getApplication();
        //ApplicationContext ac = (ApplicationContext) application.get("ac");
        //System.out.println("我们来看一下ac内存地址:" + ac);
        //this.productBysService = ac.getBean(ProductBysService1.class);
        //this.categoryService = ac.getBean(ProductBysService1.class);
    }
    @Action(value = "list", results = {
            @Result(location = "/products.jsp")
    })
    public String list() {
        products=productService.getProductsByCId(category_id);
        categoryList=productService.getAllCategories();
        return SUCCESS;
    }
    public List<Product> getProducts() {
        return products;
    }
    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public List<Category> getCategoryList() {
        return categoryList;
    }

    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }

    @Action(value = "cart", results = {
            @Result(location = "/product_summary.jsp")
    })
    public String cart() {
        System.out.println("cslfsldfs:"+product_id);
        System.out.println("sdfsdfsttt"+type);
        categoryList=productService.getAllCategories();
        Map<String, Object> session = ActionContext.getContext().getSession();
        Cart cart = (Cart) session.get("cart");
        List<Commodity>commodityList=new ArrayList<Commodity>();
        commodityList=cart.getCommodities();
        double totalPrice=cart.getTotalPrice();
        for(int i=0;i<commodityList.size();i++){
            Commodity commodity=commodityList.get(i);
            if(commodity.getProduct().getId()==product_id) {
                if (type != null) {
                    if (type.equals("c")||(type.equals("a")&&commodity.getNumber()==1)) {
                        totalPrice=(totalPrice-commodity.getNumber()*commodity.getProduct().getPrice());
                        commodityList.remove(i);
                       // cart.setCommodities(commodityList);
                        cart.setTotalPrice(totalPrice);
                        session.put("cart",cart);
                        break;
                    }else if (type.equals("b")) {
                    int number = (commodity.getNumber() + 1);
                        totalPrice=(totalPrice+commodity.getProduct().getPrice());
                        cart.setTotalPrice(totalPrice);
                    commodity.setNumber(number);
                        break;
                } else if (type.equals("a")) {
                    int number = (commodity.getNumber() - 1);
                        totalPrice=(totalPrice-commodity.getProduct().getPrice());
                        cart.setTotalPrice(totalPrice);
                    commodity.setNumber(number);
                        break;
                }
            }}}
        if(cart!=null){
            for(int i=0;i<commodityList.size();i++){
               int number= commodityList.get(i).getNumber();
                totalTax+=(commodityList.get(i).getProduct().getTax())*number;
                totalDiscount+=(commodityList.get(i).getProduct().getDiscount())*number;
            }
        }
        return SUCCESS;
    }

    public long getCategory_id() {
        return category_id;
    }
    public void setCategory_id(long category_id) {
        this.category_id = category_id;
    }

    public double getTotalDiscount() {
        return totalDiscount;
    }

    public void setTotalDiscount(double totalDiscount) {
        this.totalDiscount = totalDiscount;
    }

    public double getTotalTax() {
        return totalTax;
    }

    public void setTotalTax(double totalTax) {
        this.totalTax = totalTax;
    }

    public long getProduct_id() {
        return product_id;
    }

    public void setProduct_id(long product_id) {
        this.product_id = product_id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
